package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.CompanyCommentDao;
import ex.com.jm.food.entity.Comment;

public class CompanyCommentDaoImpl extends HibernateDaoSupport implements
		CompanyCommentDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Comment> getComments(final int initPage,final int pageRow,final String comId,
			final String userName,final String s_date,final String e_date,final String order,
			final String sort) {
		// TODO Auto-generated method stub
		Object comments=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="select new ex.com.jm.food.entity.dto.CommentDTO("
						+ "com.cmentId,"
						+ "com.cmentDate,"
						+ "com.cmentValue,"
						+ "user.userName,"
						+ "com.repDate,"
					     + "com.replaceCment)"
						+ " from Comment com "
						+ " join com.userId user "
						+ " where com.comId='"+comId+"' ";
				if (userName!=null&&!userName.equals("")) {
					hql+=" and user.userName like '%"+userName+"%'";
				}
				if (s_date!=null&&!s_date.equals("")) {
					hql+=" and com.cmentDate>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and com.cmentDate<='"+e_date+"'";
				}
				if(order!=null&&!order.equals("")&&sort!=null&&!sort.equals("")){
					hql+=" order by com.cmentDate desc";
				}
				else{
					hql+=" order by com.cmentDate desc";
				}
				//String hql="from Comment";
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<Comment>) comments;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int commentCount(final String comId,final String userName,final String s_date,
			final String e_date) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				
				String hql=""
						+ " from Comment com "
						+ " join com.userId user "
						+ " where com.comId='"+comId+"' ";
				if (userName!=null&&!userName.equals("")) {
					hql+=" and user.userName like '%"+userName+"%'";
				}
				if (s_date!=null&&!s_date.equals("")) {
					hql+=" and com.cmentDate>='"+s_date+"'";
				}
				if(e_date!=null&&!e_date.equals("")){
					hql+=" and com.cmentDate<='"+e_date+"'";
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@Override
	public Comment editComment(String cmentId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Comment.class, cmentId);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updateComment(final String cmentId,final String repment,final String repDate) {
		// TODO Auto-generated method stub
        int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="update Comment set replaceCment='"+repment+"',repDate='"+repDate+"' where cmentId='"+cmentId+"'";
				Query query=session.createQuery(hql);
				return query.executeUpdate();
			}
		});
        if(count==1){
        	return true;
        }
		return false;
	}

}
